import pandas as pd
import numpy as np

from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingClassifier as GBC
from sklearn.neural_network import MLPClassifier as MLPC
from sklearn.svm import LinearSVC, SVC

ATTRS = ['Sex', 'Age', 'Pclass', 'Parch', 'SibSp', 'Embarked']

class submission():
    def __init__(self, *args, **kwargs):
        super(submission, self).__init__(*args, **kwargs)
        self.data = pd.read_csv('kaggle/titanic/train.csv'), pd.read_csv('kaggle/titanic/test.csv')
        tr, te = self.data
        self.dummies = pd.get_dummies(tr.get(ATTRS)._append(te.get(ATTRS)), columns=ATTRS)
        self.split_index = len(tr)
    
    def submit(self, name):
        self.get_model(name)
        self.train(self.model)
        self.submit_to_file(self.model)
        
    def get_model(self, name):
        if name == 'grad':
            self.model = GBC(learning_rate=0.14)
            return
        elif name == 'mlp':
            #self.model = MLPC(hidden_layer_sizes=(21, 6), max_iter=1000)
            # self.model = MLPC(hidden_layer_sizes=(80, 19), max_iter=2000)
            # self.model = MLPC(hidden_layer_sizes=(170, 126), max_iter=2000)
            self.model = MLPC(hidden_layer_sizes=(170, 126, 12), max_iter=2000)
            return
        elif name == 'linear_svc':
            # self.model = LinearSVC(penalty='l2', C=0.3, dual='auto', max_iter=2000)
            self.model = LinearSVC(penalty='l1', C=0.6, dual='auto', max_iter=2000)
            return
        elif name == 'svc':
            # self.model = SVC(C=100, gamma=0.005)
            # self.model = SVC(C=1, gamma=0.005)
            self.model = SVC(C=100, gamma=0.0005)
            return
        raise Exception(f'no match name with {name}, need provide model name')
        
    def train(self, model):
        xtr, xte, ytr, yte = train_test_split(self.dummies[:self.split_index], self.data[0]['Survived'])
        print(model.fit(xtr, ytr).score(xte, yte))
        model.fit(self.dummies[:self.split_index], self.data[0]['Survived'])
        
    def submit_to_file(self, model):
        results = model.predict(self.dummies[self.split_index:])
        frame = pd.DataFrame({'PassengerId':self.data[1]['PassengerId'], 'Survived':results})
        frame.to_csv('kaggle/titanic/submission.csv', index=False)


if __name__ == '__main__':
    submission().submit('svc')